package com.jobportal;

import java.io.*;
import javax.servlet.*;
import java.sql.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class MyFunctions
	{
		public MyFunctions(){}
		private static Connection conn=null;
		private	static Statement stmt=null,stmt1=null;
		private static ResultSet rs=null;
		public static Connection openConnection() throws Exception
		{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jobmiller","root","root");

			return conn;
		}
		public static void closeConnection() throws Exception
		{
			conn.close();
		}
		//Function to generate Next ID for primary key in a db table
		public static String genNextID(String TableName,String IDName, String Pattern) 
			{
					String MaxID = "",NextID=Pattern;
					int MaxNum = 0;
					int L = Pattern.length();
				
					try
					 {
								openConnection();
								Statement stmt =  conn.createStatement();
								ResultSet rs = null;
								String Query = "select max(`"+IDName+"`) from `"+TableName+"`";
								String Q2 ="Select max(CAST(SUBSTRING("+IDName+","+(L+1)+",BIT_LENGTH("+IDName+")) AS UNSIGNED)) from  "+TableName;
								//System.out.println(Q2);
								rs = stmt.executeQuery(Q2);
								if(rs==null){ return Pattern+"1"; }
								if(rs.next()){ MaxID = rs.getString(1);}

									MaxNum = Integer.parseInt(MaxID);
									//System.out.println(MaxNum);
									MaxNum++;
									NextID+=MaxNum;
								closeConnection();
								if(MaxNum!=0){return NextID;}
								else {return Pattern+"1";}

					 }
					catch(Exception e)
					 {
							  System.out.println("Exception in genNextID" + e);
							  return Pattern+"1";
					 }
				}
			public static String[] MyTokenizer(String S, String P){
						StringTokenizer st = new StringTokenizer(S,P);
						int count=st.countTokens(),i=0;
						String[] Tokens = new String[count];
						while (st.hasMoreTokens())
						{
							Tokens[i] = st.nextToken();
							i++;
						}
						return Tokens;
			}
			public static String getKeywordCondition(String Keywords,String Criteria,String ColName){
				String s[] = {}; //Array of keywords
				String condition="";
				if(Keywords!=null && Keywords.trim().equals("")==false){
					if(!(Criteria.trim().equalsIgnoreCase("Exact"))){
						s = MyTokenizer(Keywords,",");
						for(int i=0;i<s.length;i++){
							condition+=ColName+" like '%"+s[i]+"%'";
							if(Criteria.trim().equalsIgnoreCase("Any") && i!=s.length-1){
							  condition+=" or ";
							}else if(Criteria.trim().equalsIgnoreCase("All") && i!=s.length-1){
							  condition+=" and ";
							}
						}
					}else{
						condition+=ColName+" like '%"+Keywords+"%'";
					}
				}
				if(condition!=null){
					return condition;
				}else{
					return "";
				}

			}
			public static String getSCondition(String Keywords,String Criteria,String Location,String Experience,String FunctionalArea){

				String SC="";
				String LocCon = "", ExpCon = "", FACon="", KeyCon="";
				if(Location!=null && Location.trim().equals("")==false){
					LocCon = "Location like '%"+Location+"%'";
					System.out.println("Location Search Condition________"+LocCon);
					SC+=LocCon+" and ";
				}
				if(Experience!=null && Experience.trim().equals("")==false){
					ExpCon = " MinExperience <="+Experience+" and MaxExperience >="+Experience;
					System.out.println("Experience Search Condition________"+ExpCon);
					SC+=ExpCon+" and ";
				}
				if(FunctionalArea!=null && FunctionalArea.trim().equals("")==false){
					FACon = "FunctionalArea like '%"+FunctionalArea+"%'";
					System.out.println("FunctionalArea Search Condition________"+FACon);
					SC+=FACon+" and ";
				}
				if(Keywords!=null && Keywords.trim().equals("")==false){
					KeyCon = getKeywordCondition(Keywords,Criteria,"JobDescription");
					System.out.println("Keyword Search Condition________"+KeyCon);
					SC+= "(( "+KeyCon+") or ";
					KeyCon = getKeywordCondition(Keywords,Criteria,"Eligbility");
					System.out.println("Keyword Search Condition________"+KeyCon);
					SC+="( "+KeyCon+")) and ";

				}
				System.out.println("Final Search Condition________"+SC);
				if(SC.length()>5){
					SC = SC.substring(0,SC.length() - 5);
				}
				return SC;
			}
	}